package com.tcm.controller.front;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.CommentUpdateDto;
import com.tcm.entity.dto.param.CommentLikeParam;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.CommentReportDto;
import com.tcm.service.CommentLikeService;
import com.tcm.service.CommentReportService;
import com.tcm.service.CommentService;
import com.tcm.utils.AccessTokenUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 学生评论控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/9/9 20:30
 */
@RestController
@RequestMapping("/stu/comment")
@RequiredArgsConstructor
public class StuCommentController {

    private final CommentService commentService;

    private final CommentLikeService commentLikeService;

    private final CommentReportService commentReportService;

    /**
     * 添加评论
     * @param dto     评论数据
     * @param request 请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addComment(@RequestBody @Valid CommentUpdateDto dto, HttpServletRequest request) {
        return commentService.addComment(dto, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 删除评论
     * @param id      评论ID
     * @param request 请求对象
     * @return 删除结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeComment(@RequestParam Long id, HttpServletRequest request) {
        return commentService.removeCommentByStu(id, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 获取评论列表
     * @param itemId  题目ID
     * @param request 请求对象
     * @return 评论列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getCommentList(@RequestParam Long itemId, HttpServletRequest request) {
        return R.success(commentService.getCommentList(itemId, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 添加评论点赞
     * @param param   评论ID
     * @param request 请求对象
     * @return 添加结果
     */
    @RequestMapping(value = "/like", method = RequestMethod.POST)
    public R addCommentLike(@RequestBody CommentLikeParam param, HttpServletRequest request) {
        return commentLikeService.addCommentLike(param.getCommentId(), AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 删除评论点赞
     * @param commentId 评论ID
     * @param request   请求对象
     * @return 处理结果
     */
    @RequestMapping(value = "/like", method = RequestMethod.DELETE)
    public R removeCommentLike(@RequestParam Long commentId, HttpServletRequest request) {
        return commentLikeService.removeCommentLike(commentId, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 举报评论
     * @param dto     举报数据DTO
     * @param request 请求对象
     * @return 处理结果
     */
    @RequestMapping(value = "/report", method = RequestMethod.POST)
    public R reportComment(@RequestBody @Valid CommentReportDto dto, HttpServletRequest request) {
        return commentReportService.reportComment(dto, AccessTokenUtil.getUserId(request), AccessTokenUtil.getUserAccount(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

}
